package connect;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.transform.Transformer;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;

import org.apache.jasper.tagplugins.jstl.core.Out;

import BDD.ConfigConnection;

/**
 * Servlet implementation class Login
 */
@WebServlet("/login")
public class Login extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	Connection connection;
	PrintWriter out;
	Transformer transfo;
	Transformer transfo2;
	ByteArrayOutputStream buff;
	TransformerHandler handler;
	StreamResult res;

	public void init(ServletConfig conf) throws ServletException {

		super.init(conf);

		try {
			connection = ConfigConnection.getConnection("connexion.properties");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		out.print("<html><head><title>Login to Gamebook</title><link href=\"/XML/style/style.css\" rel=\"stylesheet\" type=\"text/css\"/></head><body><div align= \"center\"> <img src=\"http://img714.imageshack.us/img714/3042/97821249.jpg\"/>   <h1>Please enter account information :</h1>");
		out.println("<form action=\"login\" method=\"post\" >");
		out.println("<input type=\"text\" name=\"login\"/>");
		out.println("<input type=\"password\" name=\"pwd\"/>");
		out.println("<input type=\"submit\" value=\"login\"/>");
		out.println("</form></div></body></html>");
		// TODO Auto-generated method stub
	}

	private void gotoPage(String adress, HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dispatcher = request.getRequestDispatcher(adress);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession se = request.getSession(true);

		PrintWriter out = response.getWriter();
		ResultSet rSet = null;
		String role = "";
		int equipe;
		StreamResult res = new StreamResult(out);
		PreparedStatement pStatement = null;
		try {
			pStatement = connection.prepareStatement("SELECT * FROM user WHERE Joueur_Pseudo LIKE ? AND Joueur_Pwd LIKE ?");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		try {
			pStatement.setString(2, request.getParameter("pwd"));
			pStatement.setString(1, request.getParameter("login"));
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			rSet = pStatement.executeQuery();
		} catch (SQLException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		System.out.println("--------1-------");

		try {
			rSet.next();
			role = (rSet.getString("Joueur_Role")); // TODO role
			equipe = (rSet.getInt("Joueur_Equipe"));
			se.setAttribute("role", role);
			se.setAttribute("equipe", equipe);
			gotoPage("/gamebook", request, response);
		} catch (SQLException e1) {
			out.print("<html><head><title>test</title></head><body></body></html>");
			out.println("Account information incorrect !");
			out.println("<br/><a href=\"login\">try again</a>");
		}

	}

}
